/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.shop.service.shop;

import java.util.List;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.shop.dao.shop.HkEnterShopInfoDao;
import com.thinkgem.jeesite.modules.hk.shop.dao.shop.HkUserShopDao;
import com.thinkgem.jeesite.modules.hk.shop.entity.shop.HkEnterShopInfo;
import com.thinkgem.jeesite.modules.hk.shop.entity.shop.HkUserShop;
import com.thinkgem.jeesite.modules.hk.shop.entity.shop.HkUserShopEnter;
import com.thinkgem.jeesite.modules.hk.sys.dao.user.HkUserDao;
import com.thinkgem.jeesite.modules.hk.sys.service.user.HkUserService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.hk.shop.entity.shop.HkShopReview;
import com.thinkgem.jeesite.modules.hk.shop.dao.shop.HkShopReviewDao;

/**
 * 店铺审核Service
 *
 * @author 李欣荣
 * @version 2018-10-09
 */
@Service
@Transactional(readOnly = true)
public class HkShopReviewService extends CrudService<HkShopReviewDao, HkShopReview> {

    private static final String LOGO = "/userfiles/sh.png ";


    @Autowired
    private HkUserShopDao hkUserShopDao;

    @Autowired
    private HkEnterShopInfoService hkEnterShopInfoService;

    @Autowired
    HkUserService hkUserService;

    @Autowired
    HkUserShopService hkUserShopService;

    public HkShopReview get(String id) {
        return super.get(id);
    }

    public List<HkShopReview> findList(HkShopReview hkShopReview) {
        return super.findList(hkShopReview);
    }

    public Page<HkShopReview> findPage(Page<HkShopReview> page, HkShopReview hkShopReview) {
        return super.findPage(page, hkShopReview);
    }

    @Transactional(readOnly = false)
    public void save(HkShopReview hkShopReview) {
        super.save(hkShopReview);
    }

    @Transactional(readOnly = false)
    public void delete(HkShopReview hkShopReview) {
        super.delete(hkShopReview);
    }

    @Transactional(readOnly = true, rollbackFor = Exception.class)
    public HkShopReview getShopReviewByShopId(String shopId) {
        return dao.getShopReviewByShopId(shopId);
    }


    @Transactional(readOnly = true)
    public HkShopReview getReShopReviewByLoginName(String loginName) {
        return dao.getReShopReviewByLoginName(loginName);
    }


    @Transactional(readOnly = false)
    public void saveReview(HkUserShop hkUserShop) throws Exception {


        //修改店铺表信息
        hkUserShop.setId(UserUtils.getShopId());
        hkUserShopDao.updateById(hkUserShop);


        HkShopReview hkShopReview = new HkShopReview();
        HkShopReview hkr = dao.getShopReviewByShopId(UserUtils.getShopId());

        //校验店铺是否已经存在
        if (hkr != null) {
            if (UserUtils.getShopId().equals(hkr.getShop().getId())) {
                hkShopReview.setId(hkr.getId());
            }
        }

        //向店铺审核表添加店铺信息
        hkShopReview.setShop(hkUserShop);
        hkShopReview.setReviewStatus("0");
        super.save(hkShopReview);

        //修改用户密码
        hkUserService.updatePasswordByName(UserUtils.getUser().getLoginName(), hkUserShop.getShopPassword());

    }


    /**
     * 保存商铺提交资料
     *
     * @param hkUserShopEnter
     */
    @Transactional(readOnly = false)
    public void svaeUserShopEnter(HkUserShopEnter hkUserShopEnter) throws Exception {

        HkUserShop hkUserShop = new HkUserShop();
        hkUserShop.setId(UserUtils.getShopId());

        //保存店铺管理人基本信息
        HkEnterShopInfo hkEnterShopInfo = new HkEnterShopInfo();

        HkEnterShopInfo hke = hkEnterShopInfoService.getEnterInfoByShopId(UserUtils.getShopId());
        if (hke != null) {
            hkEnterShopInfo.setId(hke.getId());
        }
        hkEnterShopInfo.setShop(hkUserShop);
        hkEnterShopInfo.setManagerName(hkUserShopEnter.getManagerName());
        hkEnterShopInfo.setManagerMobile(hkUserShopEnter.getManagerMobile());
        hkEnterShopInfo.setManagerEmail(hkUserShopEnter.getManagerEmail());
        hkEnterShopInfo.setCardPersonImgSrc(hkUserShopEnter.getCardPersonImgSrc());
        hkEnterShopInfo.setCardCountryImgSrc(hkUserShopEnter.getCardCountryImgSrc());
        hkEnterShopInfoService.save(hkEnterShopInfo);


        //保存店铺基本信息
        if (StringUtils.isNotBlank(hkUserShopEnter.getImgSrc())) {
            hkUserShop.setImgSrc(hkUserShopEnter.getImgSrc());
        } else {
            hkUserShop.setImgSrc(LOGO);
        }
        hkUserShop.setName(hkUserShopEnter.getName());
        hkUserShop.setCategory(hkUserShopEnter.getCategory());
        hkUserShop.setCreateDate(DateUtils.parseDate(DateUtils.getDateTime(), "yyyy-MM-dd HH:mm:ss"));
        hkUserShop.setUpdateDate(DateUtils.parseDate(DateUtils.getDateTime(), "yyyy-MM-dd HH:mm:ss"));
        hkUserShopDao.updateById(hkUserShop);


        HkShopReview hkShopReview = new HkShopReview();
        HkShopReview hkr = dao.getShopReviewByShopId(UserUtils.getShopId());

        //向店铺审核表添加店铺信息
        //校验店铺是否已经存在 向店铺审核表插入店铺数据
        if (hkr != null) {
            if (UserUtils.getShopId().equals(hkr.getShop().getId())) {
                hkShopReview.setId(hkr.getId());
            }
        }
        hkShopReview.setShop(hkUserShop);
        hkShopReview.setReviewStatus("1");
        hkShopReview.setReviewBeginDate(DateUtils.parseDate(DateUtils.getDateTime(), "yyyy-MM-dd HH:mm:ss"));
        hkShopReview.setReviewEndDate(DateUtils.parseDate(DateUtils.getDateTime(), "yyyy-MM-dd HH:mm:ss"));
        super.save(hkShopReview);

        //修改用户密码
        hkUserService.updatePasswordByName(hkUserShopEnter.getUsername(), hkUserShopEnter.getShopPassword());

    }


    /**
     * 返回重新申请
     */
    @Transactional(readOnly = true)
    public HkUserShopEnter registerAgain() {

        HkUserShopEnter hkUserShopEnter = new HkUserShopEnter();


        //获取店铺基本信息
        HkUserShop hkUserShop = hkUserShopDao.getShopByUserName(UserUtils.getUser().getLoginName());

        //获取店铺管理人基本信息
        HkEnterShopInfo hkEnterShopInfo = hkEnterShopInfoService.getEnterInfoByShopId(hkUserShop.getId());

        hkUserShopEnter.setManagerName(hkEnterShopInfo.getManagerName());
        hkUserShopEnter.setManagerMobile(hkEnterShopInfo.getManagerMobile());
        hkUserShopEnter.setManagerEmail(hkEnterShopInfo.getManagerEmail());
        hkUserShopEnter.setCardPersonImgSrc(Global.getUserfilesBaseDir().trim() + hkEnterShopInfo.getCardPersonImgSrc());
        hkUserShopEnter.setCardCountryImgSrc(Global.getUserfilesBaseDir().trim() + hkEnterShopInfo.getCardCountryImgSrc());


        hkUserShopEnter.setCategory(hkUserShop.getCategory());
        hkUserShopEnter.setName(hkUserShop.getName());
        hkUserShopEnter.setImgSrc(Global.getUserfilesBaseDir().trim() + hkUserShop.getImgSrc());

        return hkUserShopEnter;
    }


}